Skip to content

feat: add context api - #44

Open
legendecas wants to merge 3 commits into
nodejs:mainfrom
legendecas:context-provider
Open

legendecas wants to merge 3 commits into
nodejs:mainfrom
legendecas:context-provider

Conversation

@legendecas

@legendecas legendecas commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

This provides a reversed dependency graph in #29: microtel provides a context api that allows
OpenTelemetry to share the Trace Context and other propagated metadata like route info.

This PR demonstrates how microtel can coordinate with OpenTelemetry through a minimal API.
The API allows both microtel and OpenTelemetry to use a shared AsyncLocalStorage but with a
documented value and access contract.

This PR also allows web frameworks to provide their RPC metadata like routes
through a microtel API, without microtel instrumenting every web framework
individually.

Baggage, and suppress tracing (like do not create traces for trace exportations) are left as a TODO.

Refs: #25

@legendecas
legendecas requested a review from a team as a code owner November 7, 2026 18:28
@legendecas
legendecas force-pushed the context-provider branch 3 times, most recently from d5d321c to 789bde2 Compare November 7, 2026 18:48
Comment thread docs/context.md Resolved
Comment thread docs/context.md
Comment on lines +15 to +16
`microtel.context` holds a closed set of values, each with its own accessor.
There is no string-keyed access. Libraries that need other values keep them

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite following this. Is this saying the context object is not mutable?

@legendecas legendecas Nov 11, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a Context is a snapshot, handed from one piece of code to the next. The OpenTelemetry Context spec also requires a context to be immutable. A receiver can't change which values a Context holds. It can only make a new Context with different ones, and pass the new Context to child receiver.

The objects that a Context refers to can still be mutable. An Operation or a Span in a Context stands for the operation itself, so the receiver can update it like setting the target info or adding attributes to the spans. Maybe the context.setTarget in this API makes this confusing, I can update it.

The intention is that a context receiver must not mutate the context directly, like a receiver must not modify the TraceContext or the Baggage, because those describe this Context's position in the trace and are meant to be scoped to it.

Comment thread docs/context.md
Comment on lines +114 to +115
It can be called once per Node.js environment, such as the main thread or a
worker. A second call throws `ERR_INVALID_STATE`.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I think setAdapter is more appropriate. register implies that it can be done more than once.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setAdapter also sounds good to me. Open to API name suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants